import base64
import time
import logging
import muggle_ocr
import yaml
from common.readYaml import read_yaml
import os
import requests


def _orc_verification_code(byte):
    """
    识别验证码图片，输出文字
    :param byte: 图片数据流
    :return: 四位验证码
    """
    # 调用orc识别验证码
    sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
    for i in range(10):
        code = sdk.predict(image_bytes=byte)
        if code.isdigit():
            logging.info(f'** 识别的验证码为：{code}')
            break
    return code


def verification_code(src):
    """
    :param src:
    :return:
    """
    data = src.split('base64,')[-1]
    image_data = base64.b64decode(data)
    return _orc_verification_code(image_data)


def sleep(n_secs):
    time.sleep(n_secs)


def test_search_result(keyword, response=None):
    response_contents = response.content.get("data").get("content")

    if len(response_contents) > 10:
        print(f'搜索结果超过10条数据！')
    else:
        print(f'符合搜索的结果数是：{len(response_contents)} 条')

    for data_name in response_contents:
        if keyword in data_name['dataName']:
            print(f'搜索:{keyword},返回的数据都包含搜索内容：{data_name}')
        else:
            print(f'搜索:{keyword},返回的数据不包含搜索内容：{data_name}')
